import wx
import datetime
from wx.lib.pubsub import setuparg1
from wx.lib.pubsub import pub


import SimplePanels
from SimplePanels import PanelRecord
from DataManager import DataManager
import MainForm

from ViewRecordList import ViewRecordList
from ViewPieChart import ViewRatingPieChart, ViewMoodPieChart
from ViewTrendChart import ViewRatingTrendChart, ViewMoodTrendChart

class ViewRecord(wx.Panel):

    """
    so the purpose of this class is to switch between subviews and
    select time reference, before time reference change, model should change
    """
    def __init__(self, parent):
        wx.Panel.__init__(self, parent = parent)
        self.init_data()
        self.init_widgets()
        self.init_control()

    def init_data(self):
        self.view_mode = 'L' #L:list P:pie T:trend
        self.data_mode = 'R' #R:rating M:mood
        
        self.time_mode = SimplePanels.PANEL_VIEW_TIME_DAY
        self.time_ref = datetime.datetime.now().date()

    def init_widgets(self):
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        #a date picker
        self.pk = wx.DatePickerCtrl(
            self, size=wx.Size(100, 30),
            style=wx.DP_DROPDOWN | wx.DP_ALLOWNONE | wx.DP_SHOWCENTURY)
        #6 keys associated with 5 views
        self.v = {}
        self.v[('R','L')] = st = ViewRecordList(self)
        #self.v[('M','L')] = t #same thing
        self.v[('R','T')] = t = ViewRatingTrendChart(self)
        t.Hide()
        self.v[('M','T')] = t = ViewMoodTrendChart(self)
        t.Hide()
        self.v[('R','P')] = t = ViewRatingPieChart(self)
        t.Hide()
        self.v[('M','P')] = t = ViewMoodPieChart(self)
        t.Hide()
#        self.v.append(ViewRatingPie(self))
#        self.v.append(ViewRatingTrend(self))
#        self.v.append(ViewMotionPie(self))
#        self.v.append(ViewMotionTrend(self))

        self.sizer.Add(self.pk, 0, wx.EXPAND|wx.ALL)
        for i in self.v:
            self.sizer.Add(self.v[i], 1, wx.EXPAND|wx.ALL)
        #NOTE: this is done AFTER views were added to sizer, otherwise you will add a view twice!
        self.v[('M','L')] = st #same thing
        self.SetSizer(self.sizer)

    def init_control(self):
        self.Bind(wx.EVT_DATE_CHANGED, self.on_time_ref_change, self.pk)
        #to be more precise, time mode(day week month) changed
        pub.subscribe(self.on_time_mode_change, PanelRecord.M_TIME_CHANGE)
        pub.subscribe(self.on_view_mode_change, PanelRecord.M_VIEW_MODE_CHANGE)
        #listen to message from toolbar
        pub.subscribe(self.on_data_mode_change, MainForm.M_DATA_MODE_CHANGED)
        
    def on_view_mode_change(self, m):
        #swtich between the 3 views:list pie and trend
        #this is a listener for messages from toolbar buttons
        mode = m.data
        if self.view_mode == mode:
            #luck
            return
        else:
            self.view_mode = mode
            self.switch_views()
            
    def on_data_mode_change(self, m):
        """
        this is a listener for side button panel messages
        """
        mode = m.data
        if self.data_mode == mode:
            return
        else:
            self.data_mode = mode
            self.switch_views()

    def switch_views(self):
        k = (self.data_mode, self.view_mode)
        #first:Hide all
        for i in self.v:
            self.v[i].Hide()
        self.v[k].Show()
        #print("switch to %s,%s"%(self.data_mode, self.view_mode))
        self.sizer.Layout()
        
    def on_time_ref_change(self, evt):
        #let control handle this kind of shit
        pk = self.pk
        dt = pk.GetValue()
        if not dt.IsValid():
            return
        y = dt.Year
        m = dt.Month+1
        d = dt.Day
        new_ref= datetime.date(y,m,d)
        if self.time_ref == new_ref:
            return
        self.time_ref = new_ref

        self.model_reload()

    def on_time_mode_change(self, m):
        mode = m.data
        if self.time_mode == mode:
            return
        else:
            self.time_mode = mode

        self.model_reload()

    def model_reload(self):
        #called when time ref or time mode changed,
        #which means model will do the reload
        if self.time_mode == SimplePanels.PANEL_VIEW_TIME_DAY:
            start = self.time_ref
            end = start + datetime.timedelta(1)
        elif self.time_mode == SimplePanels.PANEL_VIEW_TIME_WEEK:
            d = self.time_ref
            start = d - datetime.timedelta(d.isoweekday())
            end = start + datetime.timedelta(7)
        elif self.time_mode == SimplePanels.PANEL_VIEW_TIME_MONTH:
            d = self.time_ref
            start = d.replace(day=1)
            if d.month != 12:
                end = d.replace(month=d.month+1, day=1)
            else:
                end = d.replace(year=d.year+1, month=1, day=1)
        #load
        DataManager.record.load(start, end)
